<template>
  <!-- 分配权限的弹层 -->
  <el-dialog
    title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
    :visible="showAssignDialog"
    @close="closeAssignDialog"
  >
    <el-tree
      ref="tree"
      :data="permTree"
      :props="{ label: 'name' }"
      :default-expand-all="true"
      :show-checkbox="true"
      :check-strictly="true"
      node-key="id"
    />
    <!-- 权限点数据展示 -->
    <template #footer>
      <el-button @click="closeAssignDialog">取消</el-button>
      <el-button type="primary" @click="submitPerm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script>
import { getPermissionList } from '@/api/permission.js'
import { tranListToTreeData } from '@/utils/index.js'
import { getRoleDetailById, assignPerm } from '@/api/setting.js'
export default {
  props: {
    showAssignDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      permTree: [],
      roleId: ''
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 完成修改，提交
    async submitPerm() {
      await assignPerm(
        { id: this.roleId,
          permIds: this.$refs.tree.getCheckedKeys()
        }
      )
      this.$message.success('操作成功！')
      this.$emit('update:showAssignDialog', false)
    },
    // 回填数据
    async recoverChecked(id) {
      this.roleId = id
      const { permIds } = await getRoleDetailById(id)
      console.log(permIds)
      this.$refs.tree.setCheckedKeys(permIds)
    },
    // 获取列表
    async getList() {
      const res = await getPermissionList()
      this.permTree = tranListToTreeData(res)
      console.log(this.permTree)
    },
    closeAssignDialog () {
      this.$emit('update:showAssignDialog', false)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
